<?php

namespace app\api\server\group;

use app\api\model\group\GroupGoodsApiModel;
use app\api\model\group\GroupOrderApiModel;
use app\api\ServerBase;
use app\logic\single\GoodsSingle;
use think\Request;

class GroupGoodsServer extends ServerBase
{
    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var GroupOrderApiModel
     */
    private $orderModel;

    function __construct(Request $request)
    {
        $this->request = $request;
        $this->param = $request->param();
        $this->model = new GroupGoodsApiModel();
        $this->orderModel= new GroupOrderApiModel();
        $this->defaultField = 'id,title,goods_id,type,img,start_time,end_time,price,original_price,is_complete,num,complete_num';
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $where['status'] = 1;
        $this->param['show_start_time'] = date('Y-m-d H:i:s');
        $this->param['show_end_time'] = date('Y-m-d H:i:s');
        $field=
        [$list,$count] = $this->model->getApiList($this->param,$where,$this->defaultField,$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->details($id,$this->defaultField.',content');
        if(empty($info)){
            $this->getMessageCode('code_common.not_find',1);
            return false;
        }
        $info['is_have'] = GoodsSingle::getInstance()->isHave($info['goods_id'],$info['type'],$this->request->user_id);
        $info['goods'] = GoodsSingle::getInstance()->getGoods($info['goods_id'],$info['type']);
        $info['is_collection'] = GoodsSingle::getInstance()->isCollection($info['goods_id'],$info['type']);
        $info['is_buy'] = $this->orderModel->isBuy($info['id'],$this->request->user_id);
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
}